package com.vipheyue.huaxue.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.vipheyue.huaxue.R;
import com.vipheyue.huaxue.adapter.BaseRecyclerAdapter;
import com.vipheyue.huaxue.adapter.BaseRecyclerViewHolder;
import com.vipheyue.huaxue.bean.EventBean;
import com.vipheyue.huaxue.utils.GsonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class SearchActivity extends AppCompatActivity {
    @Bind(R.id.iv_Menu_leftImg)
    ImageView iv_Menu_leftImg;
    @Bind(R.id.tv_Menu_leftTitle)
    TextView tv_Menu_leftTitle;
    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.iv_menu_rightTitle)
    TextView iv_menu_rightTitle;
    @Bind(R.id.iv_menu_right)
    ImageView iv_menu_right;
    @Bind(R.id.rl_menu_right)
    RelativeLayout rl_menu_right;
    @Bind(R.id.et_search)
    EditText et_search;
    @Bind(R.id.btn_search)
    Button btn_search;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.swipe_target)
    LinearLayout swipe_target;
    private BaseRecyclerAdapter adapter;
    private List<EventBean> mdata = new ArrayList<EventBean>();
    private int nowPageNum = 0;
    private int pageSize = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initview();
        setData(0, et_search.getText().toString().trim());
    }

    private void initview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerview.setLayoutManager(layoutManager);//设置显示方式
        adapter = new BaseRecyclerAdapter<EventBean>(this, mdata, R.layout.job_item_main2) {
            @Override
            public void init(BaseRecyclerViewHolder holder, EventBean bean, int position) {
                holder.setText(R.id.tv_jobTitile, bean.getTitile());
                holder.setText(R.id.tv_workAreaType, bean.getLoc());
                holder.setText(R.id.tv_princalPeople, bean.getPrincalPeople());
                holder.setText(R.id.tv_createTime, bean.getUpdatedAt());
            }
        };

        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
                EventBean job = mdata.get(position);
                String jobStr = GsonUtils.toJson(job);
                Intent intent = new Intent(SearchActivity.this, DetailMessageActivity.class);
                intent.putExtra("jobStr", jobStr);
                Logger.d(jobStr);
                startActivity(intent);
            }
        });
        recyclerview.setAdapter(adapter);//设置适配器

//        swipeToLoadLayout.setSwipeStyle(SwipeToLoadLayout.STYLE.ABOVE);

    }

    private void setData(int getNumPage, String content) {
        BmobQuery<EventBean> query = new BmobQuery<EventBean>();
        query.order("-updatedAt");
        query.setLimit(pageSize); // 限制最多10条数据结果作为一页
        query.setSkip(pageSize * getNumPage); // 忽略前10条数据（即第一页数据结果）
        query.addWhereContains("titile", content);
        query.include("releaseUser");// 希望在查询帖子信息的同时也把发布人的信息查询出来

        //TODO 这里要不要加学校限制
        query.findObjects(this, new FindListener<EventBean>() {
            @Override
            public void onSuccess(List<EventBean> object) {
                Logger.d("size...." + object.size());
                mdata.addAll(object);
                nowPageNum = nowPageNum + 1;
                // Call onRefreshComplete when the list has been refreshed.
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int code, String msg) {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.rl_menu_left, R.id.btn_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_menu_left:
                finish();
                break;
            case R.id.btn_search:
                Toast.makeText(this, "检索中,请稍候", Toast.LENGTH_SHORT).show();
                mdata.clear();
                setData(0, et_search.getText().toString().trim());
                break;
        }
    }
}
